﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/07/01
 * 
 * 功能说明：Rss读取
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using Whir.Common;
using System.Text;

public class Rss
{
    public Rss()
    {
    }

    /// <summary>
    /// 根据Url，获取Rss列表
    /// </summary>
    /// <param name="url">Url 地址</param>
    /// <param name="itemCount">获取Rss列表数目</param>
    /// <returns>返回Rss列表</returns>
    public static DataTable ProcessRSSItem(string url, int itemCount)
    {
        if (string.IsNullOrEmpty(url))
            throw new ArgumentNullException("url is null");

        if (itemCount <= 0)
            throw new ArgumentNullException("itemCount must greater than 0");

        System.Net.WebRequest myRequest = System.Net.WebRequest.Create(url);
        System.Net.WebResponse myResponse = myRequest.GetResponse();
        System.IO.Stream rssStream = myResponse.GetResponseStream();
        System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();
        rssDoc.Load(rssStream);

        System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");

        string title, link, author, pubDate, description = "";

        DataTable dt;
        //缓存Rss数据
        dt = CacheHelper.Get("rssDataTable") as DataTable;
        if (dt != null)
        {
            return dt;
        }
        else
        {
            dt = new DataTable();
            dt.Columns.Add("title", typeof(string));
            dt.Columns.Add("link", typeof(string));
            dt.Columns.Add("author", typeof(string));
            dt.Columns.Add("pubDate", typeof(string));
            dt.Columns.Add("description", typeof(string));
            DataRow row;
            for (int i = 0; i < itemCount; i++)
            {
                System.Xml.XmlNode rssDetail = rssItems.Item(i).SelectSingleNode("title");
                if (rssDetail != null)
                { title = rssDetail.InnerText; }
                else
                { title = ""; }

                rssDetail = rssItems.Item(i).SelectSingleNode("link");
                if (rssDetail != null)
                { link = rssDetail.InnerText; }
                else
                { link = ""; }

                rssDetail = rssItems.Item(i).SelectSingleNode("author");
                if (rssDetail != null)
                { author = rssDetail.InnerText; }
                else
                { author = ""; }

                rssDetail = rssItems.Item(i).SelectSingleNode("pubDate");
                if (rssDetail != null)
                { pubDate = rssDetail.InnerText; }
                else
                { pubDate = ""; }

                rssDetail = rssItems.Item(i).SelectSingleNode("description");
                if (rssDetail != null)
                { description = rssDetail.InnerText; }
                else
                { description = ""; }

                row = dt.NewRow();
                row["title"] = title;
                row["link"] = link;
                row["author"] = author;
                row["pubDate"] = pubDate;
                row["description"] = description;
                dt.Rows.Add(row);
            }
            CacheHelper.Add("rssDataTable", dt, 20);
            return dt;
        }
    }

    /// <summary>
    /// 输出Rss
    /// </summary>
    /// <returns></returns>
    public static string GetRSS(DataTable dt)
    {
        int count = dt.Rows.Count;
        StringBuilder sb = new StringBuilder();
        sb.Append("<?xml version=\"1.0\"?>");
        sb.Append("<rss version=\"2.0\">");
        sb.Append("<channel>");
        sb.Append("<title>万户网络</title>");
        sb.Append("<link>http://www.whir.net</link>");
        sb.Append("<description>万户网络</description>");
        sb.Append("<language>zh-cn</language>");
        sb.Append("<ttl>" + count + "</ttl>");
        for (int i = 0; i < count; i++)
        {
            sb.Append("<item>");
            sb.Append("<title><![CDATA[" + dt.Rows[i]["Title"] + "]]></title>");
            sb.Append("<link>" + dt.Rows[i]["link"] + "</link> ");
            sb.Append("<description><![CDATA[" + dt.Rows[i]["Description"] + "]]></description>");
            sb.Append("<pubDate>" + dt.Rows[i]["pubDate"] + "</pubDate>");
            sb.Append("<category></category>");
            sb.Append("<author>" + dt.Rows[i]["author"] + "</author>");
            sb.Append("</item>");
        }
        sb.Append("</channel>");
        sb.Append("</rss>");

        return sb.ToString();
    }
}
